﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CommonTagTime.Business;
using CommonTagTime.ModelObject;
using CommonTagTime;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Drawing;

public partial class admin_VideoDetail : SitePage
{
    public string html = string.Empty;
    private VideoCategoryCollection lstCate = new VideoCategoryCollection();

    #region Fields
    public string Command
    {

        get
        {
            object o = ViewState["Command"];
            if (o != null) return (string)o;
            return string.Empty;
        }

        set { ViewState["Command"] = value; }

    }
    public int VideoId
    {

        get
        {
            object o = ViewState["VideoId"];
            if (o != null) return (int)o;
            return int.MinValue;
        }

        set { ViewState["VideoId"] = value; }

    }
    #endregion
    #region Method
    private void InitParamater()
    {
        this.Command = clsQueryString.GetString("act");
        this.VideoId = clsQueryString.GetInt("id");
    }
    private void LoadData()
    {
        if (this.Command == "edit")
        {          
            busVideo objBus = new busVideo();
            Video objVideo = objBus.GetByID(this.VideoId);
            if (objVideo != null)
            {
                txtVideoName.Text = objVideo.Name.Trim();
                txtVideoFile.Text = objVideo.VideoFile.Trim();
                txtDescription.Text = objVideo.Description.Trim();
                if (objVideo.VideoFile != string.Empty)
                {
                    ltrVideo.Text = clsVideo.GetVideoResource(objVideo.VideoFile, 300, 200, 0, 0);
                    trVideo.Visible = true;
                }
                else trVideo.Visible = false;
                if (objVideo.IsActive)
                    chkIsActive.Checked = true;
                else chkIsActive.Checked = false;
                busVideoCategory objBusCate = new busVideoCategory();
                lstCate = objBusCate.GetByVideoId(this.VideoId);
            }
        }
    }
    private void InitData()
    {
        string validateGroup = "VideoDetail";
        vsumVideo.ValidationGroup = btnSave.ValidationGroup = reqVideoName.ValidationGroup = reqVideoFile.ValidationGroup = validateGroup;
        //Init message
        reqVideoName.ErrorMessage = string.Format(Resources.Resource.MSG_REQUIREMENT, "Name");
        reqVideoFile.ErrorMessage = string.Format(Resources.Resource.MSG_REQUIREMENT, "Video File");

    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            LoadData();
            LoadDataCategory();
        }
    }
    private void UpdateVideo(Video objVideo)
    {
        busVideo objBus = new busVideo();
        objVideo.VideoID = this.VideoId;
        if (objBus.Update(objVideo) == 1)
        {
            InsertVideoForCategory(this.VideoId, true);
            Response.Redirect("VideoList.aspx?" + this.GetPageQuery);
        }
        else lblErrorMessage.Text = Resources.Resource.MSG_UPDATE_ERROR;
    }

    private void InsertVideo(Video objVideo)
    {
        busVideo objBus = new busVideo();
        int videoID=objBus.Insert(objVideo);
        if (videoID > 0)
        {
            InsertVideoForCategory(videoID, false);
            Response.Redirect("VideoList.aspx?" + this.GetPageQuery);
        }
        else
        {
            lblErrorMessage.Text = Resources.Resource.MSG_INSERT_ERROR;
        }
    }
    private void InsertVideoForCategory(int videoID,bool isUpdate)
    {
        busVideoCategory objBus = new busVideoCategory();
        //delete old Category
        if (isUpdate)
        {
            objBus.DeleteByVideo(videoID);
        }
        string[] cate = hidCategoryIDSelect.Value.Split(';');
        if (cate.Length > 0)
        {
            int cateID;
            foreach (string s in cate)
            {
                cateID = clsConvert.Int(s);
                if (cateID > 0)
                {
                    VideoCategory objData = new VideoCategory();
                    objData.VideoID = videoID;
                    objData.CategoryID = cateID;
                    objBus.Insert(objData);
                }
            }
        }       
    }
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        InitParamater();
        InitData();
      
    }
    protected override void OnInitComplete(EventArgs e)
    {
        base.OnInitComplete(e);

    }
    private string GetSelectCheckBoxCategory(int cateId)
    {
        foreach (VideoCategory objCate in lstCate)
        {
            if (objCate.CategoryID == cateId)
            {
                lstCate.Remove(objCate);
                hidCategoryIDSelect.Value += cateId + ";";
                return "checked='checked'";
            }
        }
        return string.Empty;
    }
    private void LoadDataCategory()
    {
        bool isExpand = true;
        //Get VideoCategory
        hidCategoryIDSelect.Value = string.Empty;
        //Load all Catgeory in Level 1
        Category objCondition = new Category();
        objCondition.OrderBy = busCategory.SORTORDER;
        objCondition.OrderDirection = "ASC";
        objCondition.CurrentPage = 1;
        objCondition.PageSize = int.MaxValue;
        objCondition.Condition = " ParentId is null and Type=" + (byte)enmCategoryType.Video;
        busCategory objBus = new busCategory();
        CategoryCollection lstLevel1 = objBus.List(objCondition);//only return one object
        if (lstLevel1 != null && lstLevel1.Count > 0)
        {
            html = @"<table cellspacing='0' cellpadding='0' border='0'>
                        <tr>
                            <td style='padding:0px;'>
                                  <img height='22' width='16' src='../App_Themes/Admin/images/icons/folderplus2.gif' id='FFLD" + lstLevel1[0].CategoryID + @"'>
                            </td>
                            <td style='padding:0px;'>
                                    <img height='16' width='4' src='../App_Themes/Admin/images/icons/spacer.gif'>
                            </td>
                           
                            <td nowrap='' style='padding:0px;'><input type='checkbox' " + GetSelectCheckBoxCategory(lstLevel1[0].CategoryID) + @" id='chkCategory_" + lstLevel1[0].CategoryID + @"'  onclick=CheckCategoryItem(" + lstLevel1[0].CategoryID + @",this.checked); />
                                    <a href='javascript:void(0);' onClick=SelectCateName(" + lstLevel1[0].CategoryID + @")>" + lstLevel1[0].Name + @"</a>
                            </td>
                            </tr>
                       </table>";
            CategoryCollection lstChild = GetChild(lstLevel1[0]);
            for (int i = 0; i < lstChild.Count; i++)
            {
                Category objBreadCrum = lstChild[i];
                if (i == lstChild.Count - 1)
                    html += BuildHTML(objBreadCrum, true, isExpand);
                else html += BuildHTML(objBreadCrum, false, isExpand);
            }
        }
    }
    private CategoryCollection GetChild(Category objData)
    {
        Category objCondition = new Category();
        objCondition.OrderBy = busCategory.SORTORDER;
        objCondition.OrderDirection = "ASC";
        objCondition.Condition = " ParentId=" + objData.CategoryID;
        objCondition.CurrentPage = 1;
        objCondition.PageSize = int.MaxValue;
        busCategory objBus = new busCategory();
        CategoryCollection lstResult = objBus.List(objCondition);
        return lstResult;
    }
    private string BuildHTML(Category objData, bool lastNode,bool isExpand)
    {
        string tdPadding = string.Empty;
        string result = string.Empty;
        for (int i = 2; i < objData.Level; i++)
        {
            tdPadding += @" <td style='padding:0px;'>
                    <img height='22' width='19' src='../App_Themes/Admin/images/icons/i2.gif'>
                </td>";
        }

        //get list child for this object
        CategoryCollection lstChild = GetChild(objData);
        if (lstChild != null && lstChild.Count > 0)
        {
            string folderexpand = string.Empty;
            string expandImage = string.Empty;
            if (objData.Level > 1 && lastNode)
            {
                expandImage = "  <img height='22' width='19' id='IMG" + objData.CategoryID + @"' src='../App_Themes/Admin/images/icons/lplus2.gif'>";
            }
            else
            {
                if (isExpand)
                {
                    folderexpand = "folderminus2.gif";
                    expandImage = "  <img height='22' width='19' id='IMG" + objData.CategoryID + @"' src='../App_Themes/Admin/images/icons/tminus2.gif'>";
                }
                else
                {
                    folderexpand = "folderplus2.gif";
                    expandImage = "  <img height='22' width='19' id='IMG" + objData.CategoryID + @"' src='../App_Themes/Admin/images/icons/tplus2.gif'>";
                }
            }
            // expandImage = "  <img height='22' width='19' id='IMG" + objData.CategoryID + @"' src='../App_Themes/Admin/images/icons/lplus2.gif'>";
            result += @"<table cellspacing='0' cellpadding='0' border='0'>
            <tr>
                " + tdPadding + @"
                <td  onclick=expandit(" + objData.CategoryID + @") style='padding:0px;'>
                    " + expandImage + @"
                </td>
                <td style='padding:0px;'>
                    <img height='22' width='16' id='FLD" + objData.CategoryID + @"' src='../App_Themes/Admin/images/icons/" + folderexpand + @"'>
                </td>
                <td width='4' style='padding:0px;'>
                    <img height='16' width='4' src='../App_Themes/Admin/images/icons/spacer.gif'>
                </td>
                <td style='padding:0px;'>
                    <img height='16' width='4' src='../App_Themes/Admin/images/icons/spacer.gif'>
                </td>
                <td nowrap='' style='padding:0px;'><input type='checkbox' id='chkCategory_" + objData.CategoryID + @"' " + GetSelectCheckBoxCategory(objData.CategoryID) + @"  onclick=CheckCategoryItem(" + objData.CategoryID + @",this.checked); />
                    <a href='javascript:void(0);' onClick=SelectCateName(" + objData.CategoryID + @")>" + objData.Name + @"</a>
                </td>
            </tr>
        </table>";
            if(isExpand)
                result += "<span id='SPAN" + objData.CategoryID + "' style='display:block;'>";
            else result += "<span id='SPAN" + objData.CategoryID + "' style='display: none;'>";
            for (int index = 0; index < lstChild.Count; index++)
            {
                Category objBreadCrum = lstChild[index];
                if (index == lstChild.Count - 1)
                    result += BuildHTML(objBreadCrum, true,isExpand);
                else result += BuildHTML(objBreadCrum, false,isExpand);
            }
            result += "</span>";
        }
        else
        {
            string expandImage = string.Empty;
            if (lastNode)
            {
                expandImage = "  <img height='22' width='19'  src='../App_Themes/Admin/images/icons/l2.gif'>";
            }
            else expandImage = "  <img height='22' width='19'  src='../App_Themes/Admin/images/icons/t2.gif'>";

            result = @"<table cellspacing='0' cellpadding='0' border='0'>
            <tr>
                " + tdPadding + @"
                <td style='padding:0px;'>
                   " + expandImage + @"
                </td>
                <td style='padding:0px;'>
                    <img height='22' width='16' src='../App_Themes/Admin/images/icons/folderplus2.gif'>
                </td>
                <td width='4' style='padding:0px;'>
                    <img height='16' width='4' src='../App_Themes/Admin/images/icons/spacer.gif'>
                </td>
                <td style='padding:0px;'>
                    <img height='16' width='4' src='../App_Themes/Admin/images/icons/spacer.gif'>
                </td>
                <td nowrap='' style='padding:0px;'>
                    <input type='checkbox' id='chkCategory_" + objData.CategoryID + @"' " + GetSelectCheckBoxCategory(objData.CategoryID) + @"  onclick=CheckCategoryItem(" + objData.CategoryID + @",this.checked); />
                    <a href='javascript:void(0);' onClick=SelectCateName(" + objData.CategoryID + @")>" + objData.Name + @"</a>
                </td>
            </tr>
        </table>";
        }
        return result;
    }
    #endregion
    #region Events
    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            Video objData = new Video();
            if (chkIsActive.Checked)
                objData.IsActive = true;
            else
                objData.IsActive = false;
            objData.VideoFile = txtVideoFile.Text.Trim();
            objData.Name = txtVideoName.Text.Trim();
            objData.Description = txtDescription.Text.Trim();

            if (this.Command == "edit")
            {
                objData.UpdatedBy = this.userloginName;
                objData.UpdatedDate = clsDateTime.Now;
                UpdateVideo(objData);
            }
            else
            {
                objData.CreatedBy = this.userloginName;
                objData.CreatedDate = clsDateTime.Now;
                InsertVideo(objData);
            }
        }
        else
        {

        }
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("VideoList.aspx?" + this.GetPageQuery);
    }
    #endregion
    #region Validata
    protected void CheckSVideoValid(object source, ServerValidateEventArgs args)
    {
        args.IsValid = clsImage.IsValidVideo();
    }
    #endregion
}